﻿using elctronixMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using PagedList.Mvc;
using PagedList;

namespace elctronixMVC.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/
        electonixEntities dbef = new electonixEntities();
        electronicDataDataContext db = new electronicDataDataContext();
        public ActionResult Index()
        {
            urunler m = new urunler();
            ViewBag.Enyeni = m.urunlergetirenyeni();
            return View();
        }


        public ActionResult test()
        {
            return View();
        }

        public ActionResult urunler(int id)  // soldaki , kategorileri tıklayınca calısır
        {
            List<urunler> ulist = Models.urunler.urunlergetircatIDli(id);
            ViewBag.Urunler = ulist;
            return View();
        }

        public ActionResult urunlers(int? page)  //menüdeki ürünler tıklayınca calısır
        {
            var ulist = Models.urunler.urunlergetir();
            var pagenumber = page ?? 1;   //page bossa 1 demek
            var sayfalidata = ulist.ToPagedList(pagenumber, 9);
            ViewBag.Urunlers = sayfalidata;
            return View();

        }





        public ActionResult detaylar(int id)
        {
            urunler u = new urunler();
            u.productID = id;
            ViewBag.Urunler = u.urungetirIDLIList();
            return View();
        }



        public ActionResult teklifler()
        {
            return View();
        }






        [HttpGet]
        public ActionResult hesabim()
        {
            return View();
        }


        [HttpPost]
        public ActionResult hesabim(string email , string parola)
        {
            var collection = from u in db.uyelers
                       where u.email == email && u.parola == dateunit.MD5Sifrele(parola)
                       select u;

            foreach (var item in collection)
            {
                Session["isim"] = item.namesurname;
                Session["email"] = item.email;
                Session["userID"] = item.userID;
            }
            if (Session["userID"]==null)
            {
                return View();
            }
            else
            {
                return RedirectToAction("index","Home");
            }
            
        }




        public ActionResult uyeol()
        {
            return View();
        }





        public ActionResult kargotakip()
        {
            return View();
        }





        [HttpGet]
        public ActionResult iletisim()
        {
            List<sehirlerulkeler> s = sehirlerulkeler.ulkelerigetir();
            ViewBag.ulkeler = s;

            ViewData["durum"] = "LÜTFEN ŞİKAYETİNİZİ BİLDİRİNİZ";
            return View();
        }




        [HttpPost]
        public ActionResult iletisim(string konu,string mail,string phone , string isim ,string mesaj, bool? kabul,string cinsiyet,string ulkeler)
        {
            try
            {
                List<sehirlerulkeler> s = sehirlerulkeler.ulkelerigetir();
                ViewBag.ulkeler = s;
                
                mail m = new mail();
                m.mailadress = mail;
                m._mails = "sedattefci@hotmail.com";
                m.host = "mail.usaegitimdanismanlik.com";
                m.port = 587;
                m.login = "onurhan@usaegitimdanismanlik.com";
                m.pass = "demirdoruk1";
                string icerik = isim + "|" + phone + "|" + mesaj;
                m.icerik = icerik;
                m.subject = konu;
                m.mail_gonder();

                // return RedirectToAction("index");
                ViewData["durum"] = "basarı ile gönderildi";
            }
            catch (Exception)
            {
                
                throw;
            }
            return View();
        }




        public ActionResult signup()
        {
            
            user u = new user();
            u.email = Request.Form["txtEmail"].ToString();
            u.parola = Request.Form["txtParola1"].ToString();
            u.namesurname = Request.Form["txtAdSoyad"].ToString();
            u.telefon = Request.Form["txtTelefon"].ToString();
            u.faturaadresi = Request.Form["txtFaturaAdres"].ToString();
            u.teslimatadresi = Request.Form["txtTeslimatAdresi"].ToString();
            u.uyeekle();
            if (u.uyekayitdurum == 1)
            {
                ViewBag.durum = "Uyelik islemi basarili";
            }
            else
            {
                ViewBag.durum = "Üyelik isleminde hata olustu";
            }
            return View();
        }







        public ActionResult markalar(int id)
        {
            urunler u = new urunler();
            u.supplierID = id;
            ViewBag.Markaurunler = u.urunlergetirmarkali();
            return View();
        }






        public ActionResult sepet(int id)
        {
            // gerekli sepet islemlerini yapabilirsin
            if (Session["userID"]==null)
            {
                return RedirectToAction("hesabim","Home");
            }
            else
            {
             // sepete ekliyoruz
                siparisler s = new siparisler();
                s.userID = Session["userID"].ToString();
                s.productID = id;
                s.xmlsepetyaz();
            }
            return RedirectToAction("index");
        }





        public ActionResult urunlisteler(int? page)
        {
            
            var uruns = db.products.ToList();
            var pagenumber = page ?? 1;   //page bossa 1 demek
            var sayfalidata = uruns.ToPagedList(pagenumber, 10);
            ViewBag.Sayfali = sayfalidata;
            return View();
        }



        public ActionResult urunlerstring(string id)  // soldaki , kategorileri tıklayınca calısır
        {
            List<ikiliurun> ulist = Models.urunler.urunlergetirstring(id);
            return Json(ulist, JsonRequestBehavior.AllowGet);
        }



    }
}
